import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import { debounce, setTitle } from '../utils/helper'
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: '/h5/',
  routes
})

router.beforeEach(
  debounce((to, from, next) => {
    // 你的导航守卫逻辑
    if (to.path.startsWith('/workOrder/detail')) {
      const isInternalReferrer = document.referrer.includes(window.location.origin);
      if (isInternalReferrer) {
        to.meta.routerOrigin = 'internal'
      } else {
        to.meta.routerOrigin = 'external'
      }
    }
    next()
  }, 300)
)

router.afterEach((to) => {
  setTitle(to.meta?.title)
})

export default router
